# This is a sample Capistrano config file for EC2 on Rails.

set :application, "f-it"
set :user, "root"
set :runner, "root"
set :username, "root"

#set :deploy_via, :copy       # optional, see Capistrano docs for details
#set :copy_strategy, :export  # optional, see Capistrano docs for details

set :repository, "https://jupiterit.unfuddle.com/svn/jupiterit_ftp/trunk/"
set :scm_username,  "jupiterit_jupiterit"
set :scm_password,  "In4mation1" 

# NOTE: for some reason Capistrano requires you to have both the public and
# the private key in the same folder, the public key should have the 
# extension ".pub".
ssh_options[:keys] = %w(C:\\ec2\\fit-keypair)

# Your EC2 instances
#amazon_url = "ec2-67-202-30-36.compute-1.amazonaws.com"
amazon_url = "ec2-75-101-218-156.compute-1.amazonaws.com"
role :web,      amazon_url
role :app,      amazon_url
role :db,       amazon_url, :primary => true
role :memcache, amazon_url

set :rails_env, "production"

# EC2 on Rails config
set :ec2onrails_config, {
  # S3 bucket and subdir used by the ec2onrails:db:restore task. The subdir
  # is probably just "database"
  :restore_from_bucket => "your-bucket",
  :restore_from_bucket_subdir => "database",
  
  # S3 bucket used by the ec2onrails:db:archive task
  :archive_to_bucket => "your-other-bucket",
  
  # Set a root password for MySQL. Run "cap ec2onrails:db:set_root_password"
  # to enable this. This is optional, and after doing this the
  # ec2onrails:db:drop task won't work, but be aware that MySQL accepts 
  # connections on the public network interface (you should block the MySQL
  # port with the firewall anyway). 
  :mysql_root_password => "In4mation1",
  
  # Any extra Ubuntu packages to install if desired
  #:packages => ["logwatch", "imagemagick"],
  
  # Any extra RubyGems to install if desired: can be "gemname" or if a 
  # particular version is desired "gemname -v 1.0.1"
  #:rubygems => ["rmagick", "rfacebook -v 0.9.7"],
  
  # Set the server timezone. run "cap -e ec2onrails:server:set_timezone" for 
  # details
  #:timezone => "Canada/Eastern",
  
  # Files to deploy to the server (they'll be owned by root). It's intended
  # mainly for customized config files for new packages installed via the 
  # ec2onrails:server:install_packages task. Subdirectories and files inside
  # here will be placed in the same structure relative to the root of the
  # server's filesystem. 
  #:server_config_files_root => "../server_config",
  
  # If config files are deployed, some services might need to be restarted
  :services_to_restart => %w(apache2 postfix sysklogd),
  
  # Set an email address to forward admin mail messages to
  :admin_mail_forward_address => "brian@jupiterit.com",
  
  # Set this if you want SSL to be enabled on the web server. The SSL cert 
  # and key files need to exist on the server, The cert file should be in
  # /etc/ssl/certs/default.pem and the key file should be in
  # /etc/ssl/private/default.key (see :server_config_files_root).
  #:enable_ssl => true
}

namespace :deploy do
  task :update do
    transaction do
      update_code
      symlink
    end
    try_sudo "sudo -u root cp /mnt/app/current/config/ec2/app.yml /etc/mongrel_cluster/app.yml &"
    try_sudo "sudo -u root cp /mnt/app/current/config/ec2/mongrel /etc/init.d/mongrel &"
    try_sudo "sudo -u root a2enmod ssl &"
    try_sudo "sudo -u root a2enmod headers &"
    try_sudo "sudo -u root rm -f /etc/apache2/ports.conf &"
    try_sudo "sudo -u root touch /etc/apache2/ports.conf &"
    try_sudo "sudo -u root rm -f /etc/apache2/sites-enabled/000-default &"
    try_sudo "sudo -u root mv /etc/apache2/sites-available/app.common /etc/apache2/sites-available/app.common.old &"
    try_sudo "sudo -u root mv /etc/apache2/sites-available/default /etc/apache2/sites-available/default.old &"
    try_sudo "sudo -u root mv /etc/apache2/conf.d/app.proxy_cluster.conf /etc/apache2/conf.d/app.proxy_cluster.conf.old &"
    try_sudo "sudo -u root mkdir /etc/apache2/ssl.crt &"
    try_sudo "sudo -u root mkdir /etc/apache2/ssl.key &"
    try_sudo "sudo -u root cp /mnt/app/current/config/ec2/apache/_.jupiterit.com.crt /etc/apache2/ssl.crt/_.jupiterit.com.crt &"
    try_sudo "sudo -u root cp /mnt/app/current/config/ec2/apache/jupiterit.key /etc/apache2/ssl.key/jupiterit.key &"
    try_sudo "sudo -u root cp /mnt/app/current/config/ec2/apache/apps.conf /etc/apache2/conf.d/apps.conf &"
    try_sudo "sudo -u root cp /mnt/app/current/config/ec2/apache/fit.common /etc/apache2/conf.d/fit.common &"
    try_sudo "sudo -u root cp /mnt/app/current/config/ec2/apache/fit.secure.conf /etc/apache2/conf.d/fit.secure.conf &"
    try_sudo "sudo -u root touch /mnt/log/apache2/ftp_errors_log &"
    try_sudo "sudo -u root touch /mnt/log/apache2/ftp_log &"
    try_sudo "sudo -u root touch /mnt/log/apache2/ssl-scache.log &"
    try_sudo "sudo -u root touch /mnt/log/apache2/ssl_request_log &"
    try_sudo "cd /mnt/app/current/public && svn checkout http://javascriptmvc.googlecode.com/svn/trunk/jmvc jmvc &"
  end
  
  task :start_app do
    try_sudo "sudo -u root /etc/init.d/mongrel start &"
    try_sudo "sudo -u root /etc/init.d/apache2 start &"
  end
  
  task :stop_app do
    try_sudo "sudo -u root /etc/init.d/mongrel stop &"
    try_sudo "sudo -u root /etc/init.d/apache2 stop &"
  end
  
  task :restart_app do
    try_sudo "sudo -u root /etc/init.d/mongrel restart &"
    try_sudo "sudo -u root /etc/init.d/apache2 restart &"
  end
  
end